package com.awcall.iot.utils;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class XmlUtils {

    private XmlUtils() {
    }

    public static Document parse(String xmlStr) {
        DocumentBuilderFactory dbf = getDocumentBuilderFactory();
        StringReader sr = null;

        Document document;
        try {
            DocumentBuilder e = dbf.newDocumentBuilder();
            sr = new StringReader(xmlStr);
            InputSource is = new InputSource(sr);
            document = e.parse(is);
        } catch (ParserConfigurationException | SAXException | IOException var12) {
            throw new RuntimeException(var12);
        } finally {
            if (sr != null) {
                sr.close();
            }
        }

        return document;
    }

    public static String elementText(Element element, String name) {
        NodeList node = element.getElementsByTagName(name);
        return node.getLength() == 0 ? null : node.item(0).getTextContent();
    }

    public static String documentText(Document doc, String name) {
        NodeList node = doc.getElementsByTagName(name);
        return node.getLength() == 0 ? null : node.item(0).getTextContent();
    }

    public static Document element(Element element, String name) {
        NodeList list = element.getElementsByTagName(name);
        return list.getLength() == 0 ? null : list.item(0).getOwnerDocument();
    }

    private static DocumentBuilderFactory getDocumentBuilderFactory() {
        return DocumentBuilderFactoryHolder.INSTANCE;
    }

    public static String toXml(Map<String, String> params) {
        StringBuilder xml = new StringBuilder();
        xml.append("<xml>");
        Iterator iterator = params.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (StringUtils.isNotEmpty(value)) {
                xml.append("<").append(key).append(">");
                xml.append((String) entry.getValue());
                xml.append("</").append(key).append(">");
            }
        }

        xml.append("</xml>");
        return xml.toString();
    }

    public static Map<String, String> xmlToMap(String xmlStr) {
        Document document = parse(xmlStr);
        Element root = document.getDocumentElement();
        Map<String, String> params = new HashMap<>();
        NodeList list = root.getChildNodes();

        for (int i = 0; i < list.getLength(); ++i) {
            Node node = list.item(i);
            params.put(node.getNodeName(), node.getTextContent());
        }

        params.remove("#text");
        return params;
    }

    private static class DocumentBuilderFactoryHolder {
        private static DocumentBuilderFactory INSTANCE = DocumentBuilderFactory.newInstance();

        private DocumentBuilderFactoryHolder() {
        }
    }

}
